{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Overview\n",
    "\n",
    "This a notebook that inspects the results of a WarpX simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import statements\n",
    "import yt\n",
    "\n",
    "yt.funcs.mylog.setLevel(50)\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "%matplotlib notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read data in the simulation frame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Instruction\n",
    "\n",
    "Enter the path of the data you wish to visualize below. Then execute the cells one by one, by selecting them with your mouse and typing `Shift + Enter`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "diag_name = \"diag\"  # E.g., diagnostics.diags_names = diag\n",
    "iteration = 0\n",
    "plotfile = \"./diags/{}{:05d}\".format(diag_name, iteration)\n",
    "field = \"Ex\"\n",
    "species = \"electron\"\n",
    "ds = yt.load(plotfile)  # Load the plotfile\n",
    "# ds.field_list # Print all available quantities"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot data with yt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sl = yt.SlicePlot(ds, 2, field, aspect=0.2)  # Create a sliceplot object\n",
    "sl.annotate_particles(width=(10.0e-6, \"m\"), p_size=2, ptype=species, col=\"black\")\n",
    "sl.annotate_grids()  # Show grids\n",
    "sl.show()  # Show the plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Store quantities in numpy arrays, and plot with matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get field quantities\n",
    "all_data_level_0 = ds.covering_grid(\n",
    "    level=0, left_edge=ds.domain_left_edge, dims=ds.domain_dimensions\n",
    ")\n",
    "Bx = all_data_level_0[\"boxlib\", field].v.squeeze()\n",
    "Dx = ds.domain_width / ds.domain_dimensions\n",
    "extent = [\n",
    "    ds.domain_left_edge[ds.dimensionality - 1],\n",
    "    ds.domain_right_edge[ds.dimensionality - 1],\n",
    "    ds.domain_left_edge[0],\n",
    "    ds.domain_right_edge[0],\n",
    "]\n",
    "\n",
    "# Get particle quantities\n",
    "ad = ds.all_data()\n",
    "x = ad[species, \"particle_position_x\"].v\n",
    "z = ad[species, \"particle_position_z\"].v\n",
    "\n",
    "# Plot image\n",
    "plt.figure()\n",
    "plt.imshow(Bx[:, Bx.shape[1] // 2, :], extent=extent, aspect=\"auto\")\n",
    "plt.scatter(z, x, s=0.1, c=\"k\")"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  },
  "widgets": {
   "state": {
    "11d243e9f5074fe1b115949d174d59de": {
     "views": [
      {
       "cell_index": 6
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
